/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.common;

import hibernate.dao.AccountDAO;
import hibernate.entity.Accounts;
import hibernate.entity.Conversations;
import hibernate.entity.MessageConversations;
import hibernate.entity.Staffs;
import hibernate.entity.Students;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author Neo
 */
@ManagedBean
@SessionScoped
public class AbstractUIBean {

    public final String SMTP_ACCOUNT = "mailserverbtl6";
    public final String SMTP_PASSWORD = "iammailserver";
    public final String APP_DOMAIN = "http://localhost:8080/mailserver/faces";
    Accounts currentAccount;
    String APP_NAME = "mailserver";
    public Session hbSession = HibernateUtil.getSessionFactory().openSession();
    public Transaction hbTransaction;
    public Query hbQuery;
    public HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    public Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
    int leftStyle, topStyle;

    public void setCurrentAccount(Accounts currentAccount) {
        this.currentAccount = currentAccount;
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute("currAcc", currentAccount);
    }

    public String getAppDomain() {
        return APP_DOMAIN;
    }

    public Accounts getCurrentAccount() {
        try {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            return (Accounts) session.getAttribute("currAcc");
        } catch (Exception ex) {
            return null;
        }
    }

    public void setCurrentRole(int roleId) {
        HttpSession session = getHttpSession();
        session.setAttribute("currRoleId", roleId);
    }

    public int getCurrentRole() {
        HttpSession session = getHttpSession();
        if (session.getAttribute("currRoleId") != null) {
            return Integer.parseInt(String.valueOf(session.getAttribute("currRoleId")));
        }
        return 0;
    }

    public Staffs getCurrentStaff() {
        try {
            return getCurrentAccount().getStaffses().iterator().next();
        } catch (Exception ex) {
            return null;
        }
    }

    public HttpSession getHttpSession() {
        return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }

    public HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    }

    public HttpServletResponse getRespond() {
        return (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
    }

    public Students getCurrentStudent() {
        try {
            return getCurrentAccount().getStudentses().iterator().next();
        } catch (Exception ex) {
            return null;
        }
    }

    public void showPrimeMessage(String summary, String content) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(summary, content));
    }

    public boolean checkUserNameEmailAvailble(String username, String email) {
        AccountDAO aDao = new AccountDAO();
        if (aDao.readByUsername(username) != null) {
            showPrimeMessage("Error", "User name: " + username + " has been used");
            return false;
        }
        if (aDao.readByEmail(email) != null) {
            showPrimeMessage("Error", "Email: " + email + " has been used");
            return false;
        }
        return true;
    }

    public void redirect(String path) {
        HttpServletResponse respond = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        try {
            respond.sendRedirect(request.getContextPath() + "/faces/" + path);
        } catch (IOException ex) {
            Logger.getLogger(AbstractUIBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getOrderNumber(Conversations con) {
        Iterator<MessageConversations> it = con.getMessageConversationses().iterator();
        int max = 1;
        while (it.hasNext()) {
            MessageConversations mc = it.next();
            max = (mc.getOrderNumber() > max) ? mc.getOrderNumber() + 1 : max;
        }
        return max;
    }
    String msg_error;

    public AbstractUIBean() {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }

    public String getLeftStyleClass(int i) {
        if (i == leftStyle) {
            return "active";
        }
        return "";
    }
}
